package com.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

//    @Autowired
//    HttpServletRequest request;

//    插入操作自动填充
    @Override
    public void insertFill(MetaObject metaObject) {
      log.info("公共字段填充[insert]....");
     log.info(metaObject.toString());
     metaObject.setValue("createTime", LocalDateTime.now());
     metaObject.setValue("updateTime",LocalDateTime.now());
//     metaObject.setValue("createUser",request.getSession().getAttribute("employee"));
//     metaObject.setValue("updateUser",request.getSession().getAttribute("employee"));
//        获取线程中的用户id
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());



    }

//    更新操作自动填充
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段填充[update]....");
        log.info(metaObject.toString());
        log.info("当前线程id{}",Thread.currentThread().getId());
        metaObject.setValue("updateTime",LocalDateTime.now());

//        metaObject.setValue("updateUser",request.getSession().getAttribute("employee"));
//        获取线程中的用户id
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }
}
